//
//  XNoteBookEditViewController.m
//  iPlan
//
//  Created by 侯伟 on 2021/9/7.
//

#import "XNoteBookEditViewController.h"
#import <Realm/Realm.h>

#import "XTextField.h"

@interface XNoteBookEditViewController ()<UITextViewDelegate>

@property(nonatomic,assign)NSUInteger maxLenth;//100


@property(nonatomic,strong)MyLinearLayout *contentLayout;

/// 标题
@property(nonatomic,strong)XTextField* titleTextField ;

/// 占位文本
@property(nonatomic,strong)UILabel* placeLb;

/// 字数
@property(nonatomic,strong)UILabel* numLb;

/// content
@property(nonatomic,strong)UITextView* contentTextView;

@end

@implementation XNoteBookEditViewController

- (instancetype)init{
    if(self = [super init]){
        self.maxLenth = 200;
    }
    return self;
}

- (void)loadView{
    [super loadView];
    // 线性布局
    MyLinearLayout *rootLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    rootLayout.backgroundColor = COLOR_BACKGEOUND;
    rootLayout.myMargin = 0;
    [self.view addSubview:rootLayout];
    _contentLayout = rootLayout;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavBar];
    
    [self setupViews];
    
    if(self.operation == EditOperation_Edit){
        [self updateViews];
    }
}

- (void)setupNavBar{
    
    if(self.operation == EditOperation_Add){
        self.title = @"写笔记";
    }else{
        self.title = @"编辑笔记";
    }
    
    UIBarButtonItem* saveItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:(UIBarButtonItemStylePlain) target:self action:@selector(saveClicked:)];
    self.navigationItem.rightBarButtonItem = saveItem;
}

- (void)setupViews{
    
    
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入记录名称" attributes: @{NSForegroundColorAttributeName:COLOR_GRAY_QUITE}];
    
    XTextField* titleTextField = [[XTextField alloc] init];
    titleTextField.myHorzMargin = 16;
    titleTextField.myTop = 20;
    titleTextField.myHeight = PX(44);
    titleTextField.textField.attributedPlaceholder = attrString;
    [self.contentLayout addSubview:titleTextField];
    _titleTextField = titleTextField;
    

    MyRelativeLayout* subLayout = [MyRelativeLayout new];
    subLayout.backgroundColor = UIColor.whiteColor;
    subLayout.myHorzMargin = 16;
    subLayout.myTop = 16;
    subLayout.myHeight = MyLayoutSize.wrap;
    subLayout.layer.cornerRadius = PX(4);
    subLayout.layer.masksToBounds = YES;
    [self.contentLayout addSubview:subLayout];
    
    UITextView* textView = [UITextView new];
    textView.backgroundColor = UIColor.whiteColor;
    textView.font = FONT_FIT(14);
    textView.textColor = COLOR_BLACK_DARK;
    textView.tintColor = COLOR_THEME_A;
    textView.delegate = self;
    textView.scrollEnabled = NO;
    textView.returnKeyType = UIReturnKeyDone;
    textView.showsHorizontalScrollIndicator = NO;
    textView.showsVerticalScrollIndicator = NO;
    textView.tintColor = COLOR_THEME_A;
    textView.myMargin = 0;
    textView.myHeight = PX(128);
    [subLayout addSubview:textView];
    _contentTextView = textView;
    
    UILabel* placeLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_GRAY_QUITE text:@"给输入您想记录的内容"];
    placeLb.topPos.equalTo(textView.topPos).offset(8);
    placeLb.leftPos.equalTo(textView.leftPos).offset(8);
    [subLayout addSubview:placeLb];
    _placeLb = placeLb;
    
    UILabel* numLb = [UILabel lableWithFont:FONT(12) color:COLOR_GRAY_QUITE text:[NSString stringWithFormat:@"0/%ld",self.maxLenth]];
    numLb.myRight = 4;
    numLb.topPos.equalTo(textView.bottomPos).offset(10);
    [subLayout addSubview:numLb];
    _numLb = numLb;
}

- (void)updateViews{
    
    self.titleTextField.textField.text = self.model.title;
    
    self.contentTextView.text = self.model.content;
    self.placeLb.visibility = self.model.content.length?MyVisibility_Gone:MyVisibility_Visible;
    [self.placeLb sizeToFit];
    
    self.numLb.text = [NSString stringWithFormat:@"%ld/%ld",self.model.content.length,self.maxLenth];
    [self.numLb sizeToFit];
}

#pragma mark  --- action
///保存
- (void)saveClicked:(UIButton*)sender{
    
    
    NSString* title = self.titleTextField.textField.text;
    title = [title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    

    NSString* content = self.contentTextView.text;
    content = [content stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if(!title.length){
        [Toast toast:self.titleTextField.textField.placeholder];
        return;
    }
    if(!content.length){
        [Toast toast:self.placeLb.text];
        return;
    }
        
    if(self.operation == EditOperation_Add){

        XNoteBook* model = [XNoteBook new];
        model.title = title;
        model.content = content;
        model.update = [NSDate date];
        model.id = [NSDate date].timeIntervalSince1970;
        
        [[RLMRealm defaultRealm] beginWriteTransaction];
        [[RLMRealm defaultRealm] addObject:model];
        [[RLMRealm defaultRealm] commitWriteTransaction];
        
        if(self.editBlock){
            self.editBlock(model, self.operation);
        }

    }else if (self.operation == EditOperation_Edit){
        
        [[RLMRealm defaultRealm] beginWriteTransaction];
        self.model.title = title;
        self.model.content = content;
        self.model.update = [NSDate date];
        [[RLMRealm defaultRealm] addOrUpdateObject:self.model];
        [[RLMRealm defaultRealm] commitWriteTransaction];
        
        if(self.editBlock){
            self.editBlock(self.model, self.operation);
        }
    }
    [self closeViewControllerAnimated:YES];
}


#pragma mark  -- UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView{
    
    self.placeLb.visibility = textView.text.length?MyVisibility_Gone:MyVisibility_Visible;
    [self.placeLb sizeToFit];
    
    if(textView.markedTextRange != NULL){
        return;
    }
    
    NSString* text = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if(text.length > self.maxLenth){
        text = [text substringToIndex:self.maxLenth];
        textView.text = text;
    }
   
    self.numLb.text = [NSString stringWithFormat:@"%ld/%ld",text.length,self.maxLenth];
    [self.numLb sizeToFit];
}
@end
